<?php

namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Models\Shop;
use App\Models\ShopCrowd;
use App\Models\ShopRole;
use App\Models\ShopRolePraise;
use App\Models\UserLabel;
use App\Models\User;
use App\Models\RentClothOrder;
use App\Models\ShopCom;



/**
 * @title 分享页面
 * @description 接口说明
 */
class ShareController extends ApiController
{
    /**
     * @title 众筹项目详情
     * @description 接口说明
     * @author wjx
     * @url /api/share/shopDetail
     * @method POST
     * @param name:shop_id type:int require:1 default:1 other: desc:店铺id
     * @param name:shop_type type:int require:1 default:1 other: desc:店铺类型1线上2线下
     *
     * @return shop_price:众筹金额
     * @return has_crowd:已筹金额
     * @return promoter:发起人
     * @return address:位置
     * @return longitude:经度
     * @return latitude:纬度
     * @return area:面积
     * @return rest_time:到期时间
     * @return shop_info:店铺图片
     *
     */
    public function shopDetail(Request $request, Shop $shop, ShopCrowd $shop_crowds)
    {
        $shop_id = $request->shop_id;
        $shop_type = $request->shop_type;
        if (!$shop_id || !$shop_type) {
            return $this->failed('缺少参数');
        }

        switch ($shop_type) {
            case '1':
                $shop_detail = $shop->where(['id' => $shop_id, 'shop_type' => $shop_type])->with(['shopcrowd', 'user'])->first();
                if (!$shop_detail) {
                    $shop_detail = [];
                    return $this->success($shop_detail);
                }
                $detail = $shop_detail->transform_shop_detail_line();
                break;
            case '2':
                $shop_detail = $shop->where(['id' => $shop_id, 'shop_type' => $shop_type])->with(['shopcrowd', 'user', 'shoprole'])->first();
                if (!$shop_detail) {
                    $shop_detail = [];
                    return $this->success($shop_detail);
                }
                $detail = $shop_detail->transform_shop_detail();
                break;
            default:
                break;
        }
        // $shop_count = ShopBrowse::where(['shop_id' => $shop_id, 'user_id' => $user->id])->count();
        // if ($shop_count == 0) {
        //     ShopBrowse::create([
        //         'user_id' => $user->id,
        //         'shop_id' => $shop_id,
        //     ]);
        // }

        return $this->success($detail);
    }
    /**
     * @title 店铺角色详情
     * @description 接口说明
     * @author wjx
     * @url /api/share/shopRoleDetail
     * @method POST
     * @param name:shop_role_id type:int require:1 default:1 other: desc:角色id
     *
     * @return shop_id:店铺id
     * @return hot_spot:热度
     * @return name:名字
     * @return phone:手机
     * @return area:区域
     * @return self_introduction:介绍
     * @return img_url:图片地址
     * @return user_id:用户id
     * @return user_avatar:用户头像
     * @return is_praise:是否点赞
     * @return label:标签
     * @return praise_avatar:点赞人头像
     */
    public function shopRoleDetail(Request $request)
    {
        $data = ShopRole::where('id', $request->shop_role_id)->with('user')->first();
        if (!$data) {
            return $this->failed('参数错误');
        }
        $data->browse_num = $data->browse_num + 1;
        $data->save();
        $arr = array();
        foreach ($data['img_url'] as $k => $v) {
            $arr[] = config('app.img_url') . $v;
        }
        $detail = [];
        $detail['img_url'] = $arr;
        // $detail['user_avatar'] = config('app.img_url') . $data->user->avatar;
        $detail['user_avatar'] = $data->user->avatar;
        $detail['name'] = $data->user->nickname;
        $detail['created_at'] = $data->created_at->toDateString();
        $detail['hot_spot'] = $data->hot_spot;
        $detail['browse_num'] = $data->browse_num;
        $detail['self_introduction'] = $data->self_introduction;
        $detail['label'] = UserLabel::whereIn('id', explode(',', $data->label))->get()->pluck('name');

        $praise = ShopRolePraise::where('role_id', $request->shop_role_id)->get()->pluck('user_id');
        $praise_avatar = User::whereIn('id', $praise)->get(['id as user_id', 'avatar'])->each(function ($img) {
            $img->avatar = $img->avatar;
        });
        $detail['praise_avatar'] = $praise_avatar;
        return $this->success($detail);
    }
    /**
     * @title 所有晒单详情
     * @description 接口说明 type:晒单类型1线上2线下
     * @author wjx
     * @url /api/share/rentclothPostDetail
     * @method POST
     *
     * @param name:order_id type:int require:1  other: desc:晒单id
     * @param name:type type:int require:1  other: desc:晒单类型1线上2线下
     */
    public function rentclothPostDetail(Request $request, RentClothOrder $order)
    {
        $user = $request->user;
        $type = $request->type;
        $order_id = $request->order_id;
        if (!$type || !$order_id) {
            return $this->failed('缺少参数');
        }
        if (!in_array($type, [1, 2])) {
            return $this->failed('参数错误');
        }
        if ($type == 2) {
            $lists = $order::with(['user', 'commodity', 'commodityattr', 'brand', 'shop'])
                ->where(['status' => 2, 'type' => 2])
                ->orderByRaw(\DB::raw("id= {$order_id} desc"))
                ->get();
            $lists_line = $order::where(['id' => $order_id])->first();
            if (!$lists_line) {
                return $this->failed('晒单不存在');
            }
            $lists_line->browse_num = $lists_line->browse_num + 1;
            $lists_line->save();
            $lists = $lists->map(function ($list) {
                $img_arr = [];
                foreach ($list->img_url as $v) {
                    $img_arr[] = config('app.img_url') . $v;
                }
                $list->img_url = $img_arr;
                $list->user_avatar = $list->user->avatar;
                $list->user_nickname = $list->user->nickname;
                $list->post_time = $list->created_at->toDateString();
                $list->brand_detail = $list->brand->name;
                $list->shop_name = $list->shop->shop_name;
                $list->rentcloth_order_id = $list->id;
                return $list->only(['rentcloth_order_id', 'type', 'img_url', 'user_avatar', 'user_nickname', 'post_time', 'content', 'hot_spot', 'browse_num', 'brand_detail', 'shop_name', 'is_praise']);
            });

            return $this->success($lists);
        } else {
            $lists_line = $order::with(['user', 'commodity', 'commodityattr', 'brand', 'shop'])
                ->where(['status' => 2, 'type' => 1, 'id' => $order_id])
                ->orderBy('id', 'desc')
                ->first();
            if (!$lists_line) {
                return $this->failed('晒单不存在');
            }
            $lists_line->browse_num = $lists_line->browse_num + 1;
            $lists_line->save();
            $img_arr = [];
            foreach ($lists_line->img_url as $v) {
                $img_arr[] = config('app.img_url') . $v;
            }
            $lists_line->img_url = $img_arr;
            // $lists_line->user_avatar = config('app.img_url') . $lists_line->user->avatar;
            $lists_line->user_avatar = $lists_line->user->avatar;
            $lists_line->user_nickname = $lists_line->user->nickname;
            $lists_line->post_time = $lists_line->created_at->toDateString();
            $lists_line->brand_detail = $lists_line->brand->name;
            $lists_line->shop_name = $lists_line->shop->shop_name;
            $lists_line->rentcloth_order_id = $lists_line->id;
            $lists = $lists_line->only(['rentcloth_order_id', 'type', 'img_url', 'user_avatar', 'user_nickname', 'post_time', 'content', 'hot_spot', 'browse_num', 'brand_detail', 'shop_name', 'is_praise']);

            $shop_img = ShopCom::where('shop_id', $lists_line->shop_id)->with(['commodity'])->groupBy('commodity_id','is_buy')->limit(4)->get();
            $shop_img->transform(function ($item) {
                $item->com_img = config('app.img_url') . ($item->commodity->pictures)[0];
                return $item->only(['commodity_id', 'com_img']);
            });
            $shop_id = $lists_line->shop_id;

            return $this->success(compact('lists', 'shop_id', 'shop_img'));

        }

    }







}
